package com.tec.modules.order.rest;

import com.tec.modules.order.domain.BizOrder;
import com.tec.modules.order.service.BizOrderService;
import com.tec.modules.order.service.dto.BizOrderQueryCriteria;
import me.zhengjie.annotation.Log;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author tec
* @date 2020-05-27
*/
@Api(tags = "销售管理：销售订单")
@RestController
@RequestMapping("/api/bizOrder")
public class BizOrderController {

    private final BizOrderService bizOrderService;

    public BizOrderController(BizOrderService bizOrderService) {
        this.bizOrderService = bizOrderService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('bizOrder:list')")
    public void download(HttpServletResponse response, BizOrderQueryCriteria criteria) throws IOException {
        bizOrderService.download(bizOrderService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询订单")
    @ApiOperation("查询订单")
    @PreAuthorize("@el.check('bizOrder:list')")
    public ResponseEntity<Object> getBizOrders(BizOrderQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(bizOrderService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增订单")
    @ApiOperation("新增订单")
    @PreAuthorize("@el.check('bizOrder:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody BizOrder resources){
        return new ResponseEntity<>(bizOrderService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改订单")
    @ApiOperation("修改订单")
    @PreAuthorize("@el.check('bizOrder:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody BizOrder resources){
        bizOrderService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除订单")
    @ApiOperation("删除订单")
    @PreAuthorize("@el.check('bizOrder:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        bizOrderService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
